package com.learning.server.thread;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalDemo {

    private static ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<>();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (int i = 0; i < 100; i++) {
            String date;
            int mod = i % 60;
            if(mod < 10){
                date = "2019-11-25 09:00:0" + mod;
            }else {
                date = "2019-11-25 09:00:" + mod;
            }

            try {
                Date dateDate = sdf2.parse(date);
                //System.out.println(dateDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            executorService.submit(new DateUtil(date));
        }
    }

    static class DateUtil implements Runnable {
        private String date;

        public DateUtil(String date) {
            this.date = date;
        }

        @Override
        public void run() {
            if (sdf.get() == null) {
                sdf.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            } else {
                try {
                    Date date = sdf.get().parse(this.date);
                    System.out.println(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }

//        @Override
//        public void run() {
//            //if (sdf.get() == null) {
//            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            //} else {
//            try {
//                Date date = sdf2.parse(this.date);
//                //System.out.println(date);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            //}
//        }
    }
}
